package com.sojson.annotation.factory;

import java.lang.annotation.Annotation;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sojson.config.qps.sentinel.exception.CustomUrlBlockHandler;

/**
 * SentinelResource工厂
 * 
 * @author liu
 * @date 2021-09-10
 */
@SuppressWarnings("unchecked")
public class SentinelResourceFactory {

    /** SentinelResource的Class对象 */
    static final Class<SentinelResource> CLA = SentinelResource.class;
    /** 拦截的资源名 */
    String value;
    /** 异常处理类 */
    Class<?>[] fallbackClass = new Class[] {CustomUrlBlockHandler.class};
    String fallback = "";
    Class<Throwable>[] exceptionsToTrace = new Class[] {Throwable.class};
    Class<Throwable>[] exceptionsToIgnore = new Class[] {};
    EntryType entryType = EntryType.OUT;
    String defaultFallback = "";
    Class<?>[] blockHandlerClass = new Class[] {};
    String blockHandler = "";
    int resourceType = 0;

    public SentinelResourceFactory() {}

    public SentinelResourceFactory(String value) {
        this.value = value;
    }

    /** 获取 拦截的资源名 */
    public String getValue() {
        return value;
    }

    /** 设置 拦截的资源名 */
    public void setValue(String value) {
        this.value = value;
    }

    /** 获取 异常处理类 */
    public Class<?>[] getFallbackClass() {
        return fallbackClass;
    }

    /** 设置 异常处理类 */
    public void setFallbackClass(Class<?>[] fallbackClass) {
        this.fallbackClass = fallbackClass;
    }

    public String getFallback() {
        return fallback;
    }

    public void setFallback(String fallback) {
        this.fallback = fallback;
    }

    public Class<Throwable>[] getExceptionsToTrace() {
        return exceptionsToTrace;
    }

    public void setExceptionsToTrace(Class<Throwable>[] exceptionsToTrace) {
        this.exceptionsToTrace = exceptionsToTrace;
    }

    public Class<Throwable>[] getExceptionsToIgnore() {
        return exceptionsToIgnore;
    }

    public void setExceptionsToIgnore(Class<Throwable>[] exceptionsToIgnore) {
        this.exceptionsToIgnore = exceptionsToIgnore;
    }

    public EntryType getEntryType() {
        return entryType;
    }

    public void setEntryType(EntryType entryType) {
        this.entryType = entryType;
    }

    public String getDefaultFallback() {
        return defaultFallback;
    }

    public void setDefaultFallback(String defaultFallback) {
        this.defaultFallback = defaultFallback;
    }

    public Class<?>[] getBlockHandlerClass() {
        return blockHandlerClass;
    }

    public void setBlockHandlerClass(Class<?>[] blockHandlerClass) {
        this.blockHandlerClass = blockHandlerClass;
    }

    public String getBlockHandler() {
        return blockHandler;
    }

    public void setBlockHandler(String blockHandler) {
        this.blockHandler = blockHandler;
    }

    public int getResourceType() {
        return resourceType;
    }

    public void setResourceType(int resourceType) {
        this.resourceType = resourceType;
    }

    /**
     * 构建SentinelResource
     * 
     * @return
     */
    public SentinelResource build() {
        return new SentinelResource() {

            /**
             * SentinelResource的Class对象
             */
            @Override
            public Class<? extends Annotation> annotationType() {
                return CLA;
            }

            /**
             * 拦截的资源名
             */
            @Override
            public String value() {
                return value;
            }

            /**
             * 异常处理类
             */
            @Override
            public Class<?>[] fallbackClass() {
                return fallbackClass;
            }

            @Override
            public String fallback() {
                return fallback;
            }

            @Override
            public Class<? extends Throwable>[] exceptionsToTrace() {
                return exceptionsToTrace;
            }

            @Override
            public Class<? extends Throwable>[] exceptionsToIgnore() {
                return exceptionsToIgnore;
            }

            @Override
            public EntryType entryType() {
                return entryType;
            }

            @Override
            public String defaultFallback() {
                return defaultFallback;
            }

            @Override
            public Class<?>[] blockHandlerClass() {
                return blockHandlerClass;
            }

            @Override
            public String blockHandler() {
                return blockHandler;
            }

            @Override
            public int resourceType() {
                return resourceType;
            }
        };
    }

}